package com.xinhe.web.util;

import com.alibaba.fastjson.JSONObject;
import com.xinhe.web.common.BusinessException;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Express100Util {
   private static final String URL = "https://poll.kuaidi100.com";
   private static final String KEY = "FTNtvcOQ985";
   private static final String CUSTOMER = "E84AAFCDA017CAEAE770EBE8E6DE7165";
   private static final String SECRET = "82819eaefad64d1395821a1354756e80";
   private static final String USER_ID = "7f754b265b5a429a82b8da40a775848b";
   public static JSONObject getMapTrack(String companyCode, String number, String from, String to) {
     Map<String, String> headers = new HashMap<>();
     headers.put("Content-Type", "application/x-www-form-urlencoded");
     Map<String, String> params = new HashMap<>();
     params.put("customer", "E84AAFCDA017CAEAE770EBE8E6DE7165");
     JSONObject jsonObject = new JSONObject();
     jsonObject.put("com", companyCode);
     jsonObject.put("num", number);
     jsonObject.put("from", from);
     jsonObject.put("to", to);
     params.put("param", jsonObject.toString());
     params.put("sign", DigestUtils.md5Hex(jsonObject.toString() + "FTNtvcOQ985" + "E84AAFCDA017CAEAE770EBE8E6DE7165").toUpperCase());
     HttpEntity httpEntity = HttpClientUtil.requestPost("https://poll.kuaidi100.com/poll/maptrack.do", params, headers);
     if (httpEntity != null) {
       try {
         JSONObject respJsonObject = JSONObject.parseObject(EntityUtils.toString(httpEntity));
         String returnCode = respJsonObject.getString("returnCode");
         String status = respJsonObject.getString("status");
         if ((StringUtils.isNotBlank(returnCode) && !"200".equals(returnCode)) || (StringUtils.isNotBlank(status) && !"200".equals(status))) {
           throw new BusinessException(101,respJsonObject.getString("message"));
         }
         return respJsonObject;
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
     throw new BusinessException(102,"查询快递轨迹失败");
   }
   public static JSONObject getQuery(String companyCode, String number) {
     Map<String, String> headers = new HashMap<>();
     headers.put("Content-Type", "application/x-www-form-urlencoded");
     Map<String, String> params = new HashMap<>();
     params.put("customer", "E84AAFCDA017CAEAE770EBE8E6DE7165");
     JSONObject jsonObject = new JSONObject();
     jsonObject.put("com", companyCode);
     jsonObject.put("num", number);
     params.put("param", jsonObject.toString());
     params.put("sign", DigestUtils.md5Hex(jsonObject.toString() + "FTNtvcOQ985" + "E84AAFCDA017CAEAE770EBE8E6DE7165").toUpperCase());
     HttpEntity httpEntity = HttpClientUtil.requestPost("https://poll.kuaidi100.com/poll/query.do", params, headers);
     if (httpEntity != null) {
       try {
         JSONObject respJsonObject = JSONObject.parseObject(EntityUtils.toString(httpEntity));
         String returnCode = respJsonObject.getString("returnCode");
         String status = respJsonObject.getString("status");
         if ((StringUtils.isNotBlank(returnCode) && !"200".equals(returnCode)) || (StringUtils.isNotBlank(status) && !"200".equals(status))) {
           throw new BusinessException(102,respJsonObject.getString("message"));
         }
         return respJsonObject;
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
     throw new BusinessException(103,"实时快递单号查询失败");
   }
 }
